package com.pounds.firstgame;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;


public class SMSHelper {
	public static final String SMS_URI_ALL = "content://sms/";
	public static final String SMS_URI_INBOX = "content://sms/inbox";//收件箱
	public static final String SMS_URI_SEND = "content://sms/sent";//发件箱
	public static final String SMS_URI_DRAFT = "content://sms/draft";//草稿箱
	public static final String SMS_URI_FAILED="content://sms/failed";//发送失败
	public static final String SMS_URI_QUEUED="content://sms/queued";//代发列表，比如开启飞行模式
	
	private static SMSHelper mSmsHelper = new SMSHelper();
	private static Context mContext;
	
	private SMSHelper(){}
	
	
	public static SMSHelper getInstance(Context context){
		mContext = context;
		return mSmsHelper;
	}
	public static class Columns{
		public static final String COLUMN_ID = "_id";//一个自增字段，从1开始 
		/**
		 * ALL    = 0; 
	     * INBOX  = 1; 
	     * SENT   = 2; 
	     * DRAFT  = 3; 
	     * OUTBOX = 4; 
	     * FAILED = 5; 
	     * QUEUED = 6; 
		 */
		public static final String COLUMN_TYPE = "type"; 
		public static final String COLUMN_THREAD_ID = "thread_id";//序号，同一发信人的id相同 
		public static final String COLUMN_ADDRESS = "address";//发件人手机号码
		public static final String COLUMN_PERSON = "person";//联系人列表里的序号，陌生人为null 
		public static final String COLUMN_DATE ="date";//发件日期 
		public static final String COLUMN_PROTOCOL ="protocol";//协议，分为： 0 SMS_RPOTO, 1 MMS_PROTO  
		public static final String COLUMN_READ ="read";//是否阅读 0未读， 1已读  
		public static final String COLUMN_STATUS ="status";// 状态 -1接收，0 complete, 64 pending, 128 failed 
		public static final String COLUMN_BODY ="body";// 短信内容 
		public static final String COLUMN_SERVICE_CENTER="service_center";//短信服务中心号码编号 
	}
	
	public List<SMSInfo> getSMSList(String uri) {
		
		List<SMSInfo> smsInfos = new ArrayList<SMSInfo>();

		String[] projection = new String[] { Columns.COLUMN_ID, Columns.COLUMN_TYPE, Columns.COLUMN_THREAD_ID,
				Columns.COLUMN_ADDRESS, Columns.COLUMN_PERSON, Columns.COLUMN_DATE, Columns.COLUMN_PROTOCOL, 
				Columns.COLUMN_READ, Columns.COLUMN_STATUS,
				Columns.COLUMN_BODY, Columns.COLUMN_SERVICE_CENTER};

		Cursor cursor = mContext.getContentResolver().query(Uri.parse(uri), projection, null,
				null, "date desc");
		
		if (cursor != null) {
			while (cursor.moveToNext()) {
				SMSInfo smsInfo = new SMSInfo();
				smsInfo.id = cursor.getInt(cursor.getColumnIndex(Columns.COLUMN_ID));
				smsInfo.type = cursor.getInt(cursor.getColumnIndex(Columns.COLUMN_TYPE));
				smsInfo.threadId = cursor.getInt(cursor
						.getColumnIndex(Columns.COLUMN_THREAD_ID));
				smsInfo.address = cursor.getString(cursor
						.getColumnIndex(Columns.COLUMN_ADDRESS));
				smsInfo.person = cursor.getString(cursor
						.getColumnIndex(Columns.COLUMN_PERSON));
				smsInfo.date = cursor.getLong(cursor.getColumnIndex(Columns.COLUMN_DATE));
				smsInfo.protocol = cursor.getInt(cursor
						.getColumnIndex(Columns.COLUMN_PROTOCOL));
				smsInfo.read = cursor.getInt(cursor.getColumnIndex(Columns.COLUMN_READ));
				smsInfo.status = cursor.getInt(cursor.getColumnIndex(Columns.COLUMN_STATUS));
				smsInfo.body = cursor.getString(cursor.getColumnIndex(Columns.COLUMN_BODY));
				smsInfo.serviceCenter = cursor.getString(cursor
						.getColumnIndex(Columns.COLUMN_SERVICE_CENTER));
				smsInfos.add(smsInfo);
			}
			cursor.close();
		}
		return smsInfos;
	}
	
	public int insert(String uri,List<SMSInfo> smsInfos) {
		ContentValues[] contentValues = new ContentValues[smsInfos.size()];
		for(int i = 0; i < smsInfos.size();i++){
			ContentValues value = new ContentValues();
			SMSInfo info = smsInfos.get(i);
			value.put(Columns.COLUMN_TYPE, info.type);
			value.put(Columns.COLUMN_ADDRESS, info.address);
			value.put(Columns.COLUMN_PERSON,info.person);
			value.put(Columns.COLUMN_DATE, info.date);
			value.put(Columns.COLUMN_PROTOCOL,info.protocol);
			value.put(Columns.COLUMN_READ, info.read);

			value.put(Columns.COLUMN_STATUS, info.status);
			value.put(Columns.COLUMN_BODY, info.body);
			value.put(Columns.COLUMN_SERVICE_CENTER,info.serviceCenter);
			contentValues[i] = value;
			System.out.println("-----------" + info.body);
//			mContext.getContentResolver().insert(Uri.parse(uri), value);
		}
		
//		return 0;
		return mContext.getContentResolver().bulkInsert(Uri.parse(uri), contentValues);

	}
}
